/**
  ******************************************************************************
  * @file    common_func.h
  * @author  Sherlock
  * @version V0.1
  * @date    08/7/2024
  * @brief   Some common functions to be reused.
  ******************************************************************************
  * @copy
  *
  * <h2><center>&copy; COPYRIGHT 2010 Feirui</center></h2>
  */ 



/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef  __COMMON_FUNC_H__
#define  __COMMON_FUNC_H__


#ifdef __cplusplus
 extern "C" {
#endif


/**
  * @brief  INCLUDE FILES.
  */



/**
  * @brief GLOBALS RAM definition
  */
#ifdef   COMMON_FUNC_GLOBALS
#define  COMMON_FUNC_EXT
#else
#define  COMMON_FUNC_EXT  extern
#endif


/**
  * @brief  Structures.
  */
/* 字符串查找结构体 */
typedef struct{
  uint32_t index;
  char *adr;
} RET_FINDSUB;



/**
  * @brief  Functions.
  */
COMMON_FUNC_EXT void Comm_IntegerToString(uint8_t *Buffer, uint32_t Data, uint8_t NumLimit);
COMMON_FUNC_EXT uint16_t Comm_CalCheckSum(uint8_t *Source, uint16_t Number);
COMMON_FUNC_EXT RET_FINDSUB Comm_StrStr(char *str, const char *substring, size_t len);
COMMON_FUNC_EXT float Cal_1OrdLowPassFilter(float *lastValue, uint16_t *StartAddr, uint8_t AddrInterval, uint8_t Number, float filterCoefficient);
COMMON_FUNC_EXT uint16_t Comm_CalCRC16(uint8_t * data, uint16_t length);
COMMON_FUNC_EXT void Comm_LittleBigEndian_Convert(uint8_t mode, uint8_t *sourData, uint8_t *destData, uint8_t convertLen);
COMMON_FUNC_EXT uint16_t CalBufferDataCRC161(uint8_t *ptr, uint16_t len);
COMMON_FUNC_EXT float Comm_LowPass1OrderGetValue(float newValue, float *lastValue, float lowPassFactor);
COMMON_FUNC_EXT void Comm_CopyOriginToDestBuffer(uint16_t *originAddr, uint16_t *destAddr, uint8_t copyNum);

/*****************************************************************************************************************
 *                                                  中位数滤波
 *******************************************************************************************************************/
#define MediumFilterLenMax  5
typedef struct
{
  float FilterArr[MediumFilterLenMax];
  uint8_t currIndex;
} MediumFilterObjTypeDef;
COMMON_FUNC_EXT void Comm_MediumFilterInitStructure(MediumFilterObjTypeDef *MediumFilterStruct);
COMMON_FUNC_EXT void Comm_MediumFilterInput(MediumFilterObjTypeDef *MediumFilterStruct, float newMember);
COMMON_FUNC_EXT float Comm_MediumFilterInputAndGetValue(MediumFilterObjTypeDef *MediumFilterStruct, float newMember);
COMMON_FUNC_EXT float GetMediumNum(float *arr, uint8_t size);

/*****************************************************************************************************************
 *                                                  告警逻辑处理
 *******************************************************************************************************************/
#define AlarmOperaType_HighShut              0
#define AlarmOperaType_HighWarn              1
#define AlarmOperaType_LowWarn               2
#define AlarmOperaType_LowShut               3
#define AlarmOperaType_DisableActive         8
#define AlarmOperaType_DisableInActive       9
typedef struct
{
	uint8_t   Option   ;
	uint8_t   OperaType;
	uint8_t   OffType  ;
	float     Trip     ;
	float     Return   ;
	float     Value    ;
	uint8_t  *Status   ;
	uint16_t *Ticnt    ;
	uint16_t  HighTiLimit;
	uint16_t  LowTiLimit ;
	uint32_t *Actcnt   ;
	uint32_t  ActLimit ;
} InputAlarmSmart_ParamDef;
COMMON_FUNC_EXT uint8_t InputsAlarmSmartHandling(InputAlarmSmart_ParamDef *Param);


/*************Cfg Operation Type Define************/
#define CfgOperaType_None                   0
#define CfgOperaType_OSSem                  1
#define CfgOperaType_OSMutex                2

#define BitOperaOpt_Set        0
#define BitOperaOpt_Clr        1
#define BitOperaOpt_Nor        2

/*****************************************************************************************************************
 *                                                  三个数求最大值
 *******************************************************************************************************************/
#define GetMAXFrom3(a,b,c) (a>b?(a>c?a:c):(b>c?b:c))


#ifdef __cplusplus
}
#endif


#endif   /*  __COMMON_FUNC_H__  */

/******************* (C) COPYRIGHT 2010 Feirui *****END OF FILE****/